﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.IService;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework.Model;
using NHibernate;
using FantastPlatform.Framework.Common;
using FantastPlatform.Framework.Exception;
using NHibernate.Criterion;

namespace FantastPlatform.Framework.Service
{
   internal class EmployeePostService:DataService<IEmployeePost,EmployeePost>,IEmployeePostService
    {
       public override object Create(IEmployeePost entity)
       {
           object id = 0;
           ITransaction transcation = null;
           TransactionDelegate tran =(ITransaction trans) =>
               {
                  DefaultValue(entity);
                   if (entity.PreviousVersion == null)
                   {
                       IEmployeePost employeePost=Session.CreateCriteria(typeof(EmployeePost)).Add(Expression.Eq("Employee.Id",entity.Employee.Id))
                           .Add(Expression.Eq("Post.Id",entity.Post.Id)).UniqueResult<IEmployeePost>();
                       Check.Require<ObjectExistedException>(employeePost == null, string.Format("员工岗位已存在！"));
                       entity.PreviousVersion = entity;
                       entity.VersionNo = 1;
                       id = Session.Save(entity);
                   }
                   else
                   {
                       entity.VersionNo = entity.PreviousVersion.VersionNo + 1;
                       id = Session.Save(entity);
                   }
               };
           ExecuteTransaction(tran, transcation);
           return id;
       } 
    }
}
